﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;
using System.Collections.Generic;

public partial class System_Other_BannerModify : BasePage
{
    t_bannerBLL bll = new t_bannerBLL();
    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");

        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            getDate(Id);
        }
    }
    #endregion

    #region 加载数据
    private void getDate(int id)
    {
        if (Action == "Update")
        {
            t_banner model = new t_banner();
            model = bll.GetModel(id);
            this.txt_Title.Text = model.Title;
            this.txt_LinkHref.Text = model.LinkHref;
            this.txt_OrderNo.Text = model.OrderNo.ToString();
            this.radio_Valid.SelectedValue = model.Valid.ToString();
            this.img_Image.Src = model.ImgSrc;
            this.hd_Image.Value = model.ImgSrc;
          
        }
        //else
        //{
        //    this.txt_ReleaseDate.Text = DateTime.Now.ToString();
        //}
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        if (Action == "Update")
        {
            #region 修改
            try
            {
                t_banner model = new t_banner();

                model = bll.GetModel(Id);

                if (model == null)
                {
                    base.Alert("操作失败！");
                    return;
                }

                model.Title = this.txt_Title.Text.Trim();
                model.ImgSrc = this.hd_Image.Value.Trim();
                model.LinkHref = this.txt_LinkHref.Text.Trim();
                model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);

                bll.Update(model);

                base.Redirect("操作成功！", "BannerList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }
        else
        {
            #region 新增
            try
            {
                int bannerCount = new UtilityBLL().GetRecordCount("t_banner", "1=1");
                
                if (bannerCount > 10)
                {
                    base.Alert("最多只能添加10条记录！");
                    return;
                }

                t_banner model = new t_banner();

                model.Title = this.txt_Title.Text.Trim();
                model.ImgSrc = this.hd_Image.Value.Trim();
                model.LinkHref = this.txt_LinkHref.Text.Trim();
                model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                
                bll.Add(model);

                base.Redirect("操作成功！", "BannerList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }

    }
    #endregion
}
